﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Data;
using RuleCode.Common;

namespace map.member.dialogs
{
    public partial class ItemSeller_listcate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //AliApi.appUserId = "maxfara@163.com";
            AjaxPro.Utility.RegisterTypeForAjax (typeof (AjaxFunction ));
            if (!IsPostBack)
            {
                BindMainDataList();
            }
            else
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "initframe", "<script type=\"text/javascript\">if(typeof(initFrameHeight)!='undefined')initFrameHeight();</script>", false);
        }

        #region 辅助方法
        private void BindMainDataList()
        {
            this.NoDataDiv.Visible = true;
            if (AliApi.appUserId != string.Empty)
            {
                string sqlText = "select * from taobaoitemcat where taobaouserid='" + AliApi.appUserId + "' and taobaoparentcid='0' order by taobaosort_order asc";
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
                if (dSet.Tables.Count > 0 )
                {
                    this.MainDataList.DataSource = dSet.Tables[0];
                    this.MainDataList.DataBind();
                }
                
                this.NoDataDiv.Visible = (dSet.Tables .Count >0 && dSet.Tables [0].Rows.Count >0) ?false  :true  ;
                //taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaouserid='"+AliApi.appUserId +"' and taobaoparentcid='0'","taobaosort_order asc");
                //this.MainDataList.DataSource = taobaoitemcate.MyTable;
                //this.MainDataList.DataBind();
                //if(taobaoitemcate.Count >0)
                //this.NoDataDiv.Visible = false ;
            }
        }

        private void BindDetailDataList(string parentCid,DataList detailDataList,DataListItem item)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string sqlText = "select * from taobaoitemcat where taobaouserid='" + AliApi.appUserId + "' and taobaoparentcid='"+parentCid +"' order by taobaosort_order asc";
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
                if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
                {
                    detailDataList.DataSource = dSet.Tables[0];
                    detailDataList.DataBind();
                    (item.FindControl("cateitemsAnchor") as HtmlAnchor).Visible = false;
                }                    
            }
        }

        public string GetMoveCateSelectHtml(string currentCateId)
        {                            
            string result = string.Empty;
            result += "<select onchange=\"moveCateSelectedChanged(this);\" style=\" width:130px;\">";
            result +="<option value=\"\" selected=\"true\" >--请选择--</option>";
            if (AliApi.appUserId != null)
            {
                taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaouserid ='"+AliApi.appUserId +"' and taobaoparentcid='0'","taobaosort_order asc");
                for (int i = 0; i < taobaoitemcate.Count; i++)
                {
                    if (taobaoitemcate.taobaocid.Value != currentCateId) 
                        result += "<option value=\""+taobaoitemcate.taobaocid .Value +"\"  >"+taobaoitemcate .taobaoname.Value +"</option>";
                    taobaoitemcate.Next();
                }
            }
            result += "</select>";
            return result;
        }

        public string getMoveUpAnchorHTML(int sort_order,string currentCateId,string parentCid)
        {
            string html = string.Empty;
            string titlePrefix = (parentCid == "0") ? "父级分类" : "子级分类";
            if (sort_order == 1)
            {
                html = "<a href=\"#\" title=\""+titlePrefix +"向上移动\" class=\"ItemCateOprButton\" style=\" background-position:-23px -102px;cursor:not-allowed\"> </a>";
            }
            else
            {
                html = "<a href=\"#\" title=\"" + titlePrefix + "向上移动\" onclick=\"moveCateId('" + currentCateId + "',true);\" onmouseover=\"this.style.backgroundPosition='0px -34px';\" onmouseout =\"this.style.backgroundPosition='-23px -34px';\" class=\"ItemCateOprButton\" style=\" background-position:-23px -34px;\"> </a>";
            }
            return html;
        }
        public string getMoveDownAnchorHTML(int sort_order, string currentCateId, string parentCid)
        {
            string html = string.Empty;
            string titlePrefix = (parentCid == "0") ? "父级分类" : "子级分类";
            int totalCount = GetChildCateTotalCount (parentCid);
            if(sort_order ==totalCount )                                      
            {
                html = "<a href=\"#\" title=\"" + titlePrefix + "向下移动\"  class=\"ItemCateOprButton\" style=\" background-position:-23px -85px;cursor:not-allowed\"> </a>";
            }
            else
            {
                html = "<a href=\"#\" title=\"" + titlePrefix + "向下移动\" onclick=\"moveCateId('" + currentCateId + "',false);\" onmouseover=\"this.style.backgroundPosition='0px -17px';\" onmouseout =\"this.style.backgroundPosition='-23px -17px';\" class=\"ItemCateOprButton\" style=\" background-position:-23px -17px;\"> </a>";
            }
            return html;
        }
         
                                       
        public static  int GetChildCateTotalCount(string parentCateId)
        {
            int result = 0;
            if (AliApi.appUserId != string.Empty)
            {
                taobaoItemCat taobaoitemcate = taobaoItemCat.Find("taobaouserid='"+AliApi.appUserId +"' and taobaoparentcid='"+parentCateId +"'");
                result = taobaoitemcate.Count;
            }
            return result;
        }
        #endregion

        protected void UpdatePageButton_Click(object sender, EventArgs e)
        {
            BindMainDataList();
        }

        protected void MainDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item)
            {
                HtmlInputHidden taobaoCidHidden = e.Item.FindControl("taobaoCidHidden") as HtmlInputHidden;
                DataList detailDataList =e.Item.FindControl ("DetailDataList") as DataList ;
                string parentcid = taobaoCidHidden.Value;
                if (AliApi.appUserId != string.Empty && parentcid.Trim () != string.Empty)
                {
                    BindDetailDataList(parentcid, detailDataList, e.Item);
                }
            }
        }

        protected void MainDataList_DataBinding(object sender, EventArgs e)
        {
           
        }



    }
}
